package yhp.com.zijie.service.impl;

import yhp.com.zijie.bean.Menu;
import yhp.com.zijie.dao.MenuDao;
import yhp.com.zijie.dao.impl.MenuDaoImpl;
import yhp.com.zijie.service.MenuService;

import java.util.ArrayList;
import java.util.List;

public class MenuServiceImpl implements MenuService {

    private MenuDao menuDao = new MenuDaoImpl();

    @Override
    public List<Menu> getMenus() {

        List<Menu> menuList = menuDao.getMenus();
        List<Menu> newMenuList = new ArrayList<>();

        for (Menu menu : menuList) {
            if (menu.getUpMenuId()==0){//说明是一级菜单，

                ArrayList<Menu> secondList = new ArrayList<>();//一级菜单下有多个二级菜单

                for (Menu second : menuList) { //再次遍历，找出二级菜单
                    if (second.getUpMenuId()==menu.getMenuId()){
                        secondList.add(second);
                    }
                }

                menu.setSecondMenuList(secondList);
                newMenuList.add(menu);
            }
        }


        return newMenuList;
    }
}
